package com.opticon.vaadinR.core.r;

import com.google.gson.Gson;
import com.opticon.vaadinR.core.json.JSONDensityResultDescriptor;
import com.opticon.vaadinR.factory.TabFactory;
import com.opticon.vaadinR.ui.panel.SimplePNGTab;
import com.opticon.vaadinR.ui.panel.SimpleTextTab;
import com.vaadin.Application;

import java.io.BufferedReader;
import java.io.File;

/**
 * This class is a
 */
public class DensityRResultHandler extends AbstractRResultHandler {

    protected JSONDensityResultDescriptor iJSONResultDescriptor;

    public DensityRResultHandler(Application aApplication) {
        super(aApplication);
//        iApplication = aApplication;
    }

    @Override
    protected void loadJSON(BufferedReader aReader) {
        iJSONResultDescriptor = new Gson().fromJson(aReader, JSONDensityResultDescriptor.class);
    }


    @Override
    public void handleImages() {
        File lFile = new File(iJSONResultDescriptor.getDir(), iJSONResultDescriptor.getImage().getFile());
        String lCaption = iJSONResultDescriptor.getImage().getTitle();

        SimplePNGTab lSimplePNGTab = TabFactory.makeGraphTab(lFile, getApplication());
        lSimplePNGTab.setCaption(lCaption);

        iDashboard.addTab(lSimplePNGTab);
    }

    @Override
    public void handleDataFrame() {
        // not yet implemented
    }

    @Override
    public void handleText() {
        File lFile = new File(iJSONResultDescriptor.getDir(), iJSONResultDescriptor.getTxt().getFile());
        String lCaption = iJSONResultDescriptor.getTxt().getTitle();

        SimpleTextTab lSimpleTextTab = TabFactory.makeTextTab(lFile, getApplication());
        lSimpleTextTab.setCaption(lCaption);

        iDashboard.addTab(lSimpleTextTab);

    }
}
